package com.njpi.stu.attendance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.njpi.stu.attendance.entity.*;
import com.njpi.stu.attendance.entity.Class;
import com.njpi.stu.attendance.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * <p>
 * 假条表（只要该课程的任课老师审核通过即可） 前端控制器
 * </p>
 *
 * @author pxl
 * @since 2019-11-06
 */
@Controller
@RequestMapping("/leave-note")
public class LeaveNoteController {

    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IClassService classService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private ILeaveNoteService leaveNoteService;

    @Autowired
    private  IUserService userService;

    /**
     * 根据院级查询所有的班级
     * @param model
     * @param departmentName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/class/{dep}",method = RequestMethod.GET)
    public String getClassByDep(Model model, @PathVariable("dep") String departmentName){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("department_name",departmentName);
        List<Class> classList = classService.list(queryWrapper);
        Gson gson=new Gson();
        String result = gson.toJson(classList);
        return result;
    }

    /**
     * 根据院级查询所有的课程
     * @param model
     * @param departmentName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/course/{dep}",method = RequestMethod.GET)
    public String getCourseByDep(Model model, @PathVariable("dep") String departmentName){
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("department_name",departmentName);
        Department department = departmentService.getOne(queryWrapper1);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("par_course_id",department.getDepartmentId());
        List<Course> courseList = courseService.list(queryWrapper);
        Gson gson=new Gson();
        String result = gson.toJson(courseList);
        return result;
    }

    /**
     * 初始化申请假条页面
     * @param model
     * @param session
     * @return
     */
    @RequestMapping(value = "/apply",method = RequestMethod.GET)
    public String apply(Model model, HttpSession session){
        model.addAttribute("title","申请假条");
        //查找所有学院
        List<Department> depList = departmentService.list();
        model.addAttribute("depList",depList);
        //查找所有班级
        List<Class> classList = classService.list();
        model.addAttribute("classList",classList);
        //查找所有课程
        List<Course> courseList = courseService.list();
        model.addAttribute("courseList",courseList);
        //查找学院
        User user = (User) session.getAttribute("user");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("class_name",user.getClassName());
        Class aClass = classService.getOne(queryWrapper);
        session.setAttribute("dep",aClass.getDepartmentName());


        return "leaveNote";
    }


    /**
     *申请假条
     * @param leaveNote
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/apply",method =RequestMethod.POST)
    public String saveLeaveNote(@RequestBody LeaveNote leaveNote){
        leaveNote.setState(0);
        leaveNote.setTimeStudent(new Date());
        boolean save = leaveNoteService.save(leaveNote);

        return "假条已申请";
    }


    /**
     * 跳转批准假条页面
     * @param model
     * @return
     */
    @RequestMapping("/appro/{currentPage}")
    public  String skipApproPage(Model model,@PathVariable("currentPage") String currentPage){
        int current=Integer.valueOf(currentPage);
        model.addAttribute("title","批准假条");
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("user_num",name);
        User user = userService.getOne(queryWrapper1);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("class_name",user.getClassName());
        queryWrapper.orderByAsc("state");
        Page page=new Page();
        page.setCurrent(current);
        page.setSize(3);
        IPage iPage = leaveNoteService.page(page, queryWrapper);
        //计算出最大页数
        int maxPage= (int) (iPage.getTotal()/iPage.getSize());
        if((int)(iPage.getTotal()%iPage.getSize())>0){
            maxPage=maxPage+1;
        }
        model.addAttribute("maxPage",maxPage);
        model.addAttribute("result",iPage);
        return "appro";
    }


    /**
     *老师审核假条
     * @param model
     * @param updateLeaveNote
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/appro",method = RequestMethod.PUT)
    public String updateLeaveNote(Model model,@RequestBody UpdateLeaveNote updateLeaveNote){
        for (int i = 0; i < updateLeaveNote.getIds().length; i++) {
            LeaveNote leaveNote=new LeaveNote();
            leaveNote.setTeacherName(updateLeaveNote.getTeacherName());
            leaveNote.setTimeTeacher(new Date());
            leaveNote.setId(updateLeaveNote.getIds()[i]);
            leaveNote.setState(updateLeaveNote.getState());
            leaveNoteService.updateById(leaveNote);
        }
        return "审核成功";
    }

    /**
     * 跳转假条记录页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/log/{currentPage}",method = RequestMethod.GET)
    public String skipLeaveNoteLogPage(Model model,@PathVariable("currentPage") String currentPage){
        int current=Integer.valueOf(currentPage);
        model.addAttribute("title","假条记录");
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",name);
        queryWrapper.orderByAsc("state");
        Page page=new Page();
        page.setCurrent(current);
        page.setSize(3);
        IPage iPage = leaveNoteService.page(page, queryWrapper);
        //计算出最大页数
        int maxPage= (int) (iPage.getTotal()/iPage.getSize());
        if((int)(iPage.getTotal()%iPage.getSize())>0){
            maxPage=maxPage+1;
        }
        model.addAttribute("maxPage",maxPage);
        model.addAttribute("result",iPage);

        return "/leaveNoteLog";
    }





    /**
     * 跳转假条详细页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/Onelog/{id}",method = RequestMethod.GET)
    public String Onelog(Model model,@PathVariable("id") String id){
        LeaveNote leaveNote = leaveNoteService.getById(Long.valueOf(id));
        model.addAttribute("leaveNote",leaveNote);
        return "/leaveNoteOneLog";
    }

}
